
using System;
using System.Collections.Generic;
using System.Data;

namespace AutoTool
{
    public class UserStorageRepository
    {
        private readonly DatabaseHelper _dbHelper;

        public UserStorageRepository(string connectionString)
        {
            _dbHelper = new DatabaseHelper(connectionString);
        }

        public List<UserStorage> GetAllUserStorage()
        {
            string query = "SELECT * FROM UserStorage";
            DataTable result = _dbHelper.ExecuteQuery(query);
            return DataMapper.MapToList<UserStorage>(result);
        }

        public UserStorage GetUserStorageById(Guid id)
        {
            string query = "SELECT * FROM UserStorage WHERE StorageId = @StorageId";
            var parameters = new Dictionary<string, object>
            {
                { "@StorageId", id }
            };

            DataTable result = _dbHelper.ExecuteQuery(query, parameters);
            if (result.Rows.Count > 0)
            {
                return DataMapper.MapToObject<UserStorage>(result.Rows[0]);
            }
            return null;
        }

        public void InsertUserStorage(UserStorage entity)
        {
            string query = "INSERT INTO UserStorage (StorageId, UserId, TotalSpace, UsedSpace, PlanType, CreateTime, ExpireTime) VALUES (@StorageId, @UserId, @TotalSpace, @UsedSpace, @PlanType, @CreateTime, @ExpireTime)";
            var parameters = new Dictionary<string, object>
            {
                { "@StorageId", entity.StorageId },
                { "@UserId", entity.UserId },
                { "@TotalSpace", entity.TotalSpace },
                { "@UsedSpace", entity.UsedSpace },
                { "@PlanType", entity.PlanType },
                { "@CreateTime", entity.CreateTime },
                { "@ExpireTime", entity.ExpireTime }
            };

            _dbHelper.ExecuteNonQuery(query, parameters);
        }

        public void DeleteUserStorage(Guid id)
        {
            string query = "DELETE FROM UserStorage WHERE StorageId = @StorageId";
            var parameters = new Dictionary<string, object>
            {
                { "@StorageId", id }
            };

            _dbHelper.ExecuteNonQuery(query, parameters);
        }
    }
}